<?php

namespace app\api\controller;
use app\admin\model\Article;
use app\common\controller\Api;
use app\manystore\model\ManystoreShop;

/**
 * 门店
 */
class Wish extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = '*';
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Wish();
    }

    //心愿提报
    public function addwish(){
        $data=input('post.');
        if($data['game_name']==""||$data['game_name']==null){
            $this->error('请输入游戏名称', []);
        }
        if($data['feedback_text']==""||$data['feedback_text']==null){
            $this->error('请输入心愿描述', []);
        }
        if($data['media_urls']==""||$data['media_urls']==null){
            $this->error('请输入附图', []);
        }
        if($data['shop_id']==""||$data['shop_id']==null){
            $this->error('请选择门店', []);
        }
        $result = $this->model::create([
            'user_id' => $this->auth->id,
            'game_name' => $data['game_name'],
            'shop_id'=>$data['shop_id'],
            'feedback_text' => $data['feedback_text'],
            'media_urls'=>$data['media_urls'],
        ]);
        $this->success('提交成功', []);
    }

    /*
     * 提报记录
     */
    public function wish_list(){
        $list=$this->model->where('user_id','=',$this->auth->id)->order('id desc')->paginate(10);
        foreach ($list as $item) {
            $item['media_urls']= explode(',', $item['media_urls']);
        }
        $this->success('请求成功',$list);
    }

    /*
     * 提报详情
     */

    public function wish_info(){
        if (!$id = input('id/d')) {
            $this->error(__('请选择记录'));
        }
        $info=$this->model->where('id','=',$id)->find();
        $info['media_urls'] = $info['media_urls']==""?[]:explode(',', $info['media_urls']);
        $this->success('请求成功',$info);
    }

    /*
     * 心愿箱
     */
    public function wish_artice()
    {
        $info = Article::where('type', '=', 'wish')->field('content,video_url,carousel')->find();
        if($info){

            $imagesArray = explode(',', $info['carousel']);
            $resultArray = [];
            foreach ($imagesArray as $url) {
                $resultArray[] = ['image' => trim($url)]; // 使用 trim 去除多余的空格
            }
            $info['carousel'] = $resultArray;
            $info['video_url'] = $info['video_url']==""?[]:explode(',', $info['video_url']);
        }
        $this->success('请求成功', $info);
    }



}
